package hn.cch.spring.aop.proxy;


import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Arrays;

public class CglibImpl implements MethodInterceptor {

    public ProxyApi proxyApi;

    public CglibImpl() {
    }

    public CglibImpl(ProxyApi proxyApi) {
        this.proxyApi = proxyApi;
    }

    public ProxyApi getProxyInterface() {
        return proxyApi;
    }

    public void setProxyInterface(ProxyApi dynamicProxyApi) {
        this.proxyApi = dynamicProxyApi;
    }


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before:" + Arrays.toString(objects));
        // Object invoke = method.invoke(dynamicProxyApi, objects); // 使用反射机制
        Object invoke = methodProxy.invoke(proxyApi, objects); // 不推荐使用，否则死循环
        System.out.println("after:" + invoke);
        return invoke;
    }



}
